﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TestPrinter
{
    public partial class Form1 : Form
    {

        // Declare the PrintDocument object.
        private System.Drawing.Printing.PrintDocument docToPrint =
            new System.Drawing.Printing.PrintDocument();
        
        private Font printFont;
        private StreamReader streamToPrint;

        // Declare a PrintDocument object named document.
        private System.Drawing.Printing.PrintDocument document =
            new System.Drawing.Printing.PrintDocument();
        public Form1()
        {
            InitializeComponent();

            // Associate the event-handling method with the 
            // document's PrintPage event.
            this.document.PrintPage += document_PrintPage;

            this.printDocument1.PrintPage += document_PrintPage;
        }

        // The PrintDialog will print the document
        // by handling the document's PrintPage event.
        private void document_PrintPage(object sender,
            System.Drawing.Printing.PrintPageEventArgs e)
        {

            // Insert code to render the page here.
            // This code will be called when the control is drawn.

            // The following code will render a simple
            // message on the printed document.
            string text = "In document_PrintPage method. ";
            text = File.ReadAllText(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\MyFile.txt");
            System.Drawing.Font printFont = new System.Drawing.Font
                ("Arial", 35, System.Drawing.FontStyle.Regular);

            // Draw the content.
            e.Graphics.DrawString(text, printFont,
                System.Drawing.Brushes.Black, 10, 10);
        }

        //This method displays a PageSetupDialog object. If the
        // user clicks OK in the dialog, selected results of
        // the dialog are displayed in ListBox1.
        private void button1_Click(object sender, EventArgs e)
        {
            // Initialize the dialog's PrinterSettings property to hold user
            // defined printer settings.
            pageSetupDialog1.PageSettings =
                new System.Drawing.Printing.PageSettings();

            // Initialize dialog's PrinterSettings property to hold user
            // set printer settings.
            pageSetupDialog1.PrinterSettings =
                new System.Drawing.Printing.PrinterSettings();

            //Do not show the network in the printer dialog.
            pageSetupDialog1.ShowNetwork = false;

            //Show the dialog storing the result.
            DialogResult result = pageSetupDialog1.ShowDialog();

            // If the result is OK, display selected settings in
            // ListBox1. These values can be used when printing the
            // document.
            if (result == DialogResult.OK)
            {
                object[] results = new object[]{
            pageSetupDialog1.PageSettings.Margins,
            pageSetupDialog1.PageSettings.PaperSize,
            pageSetupDialog1.PageSettings.Landscape,
            pageSetupDialog1.PrinterSettings.PrinterName,
            pageSetupDialog1.PrinterSettings.PrintRange};
                listBox1.Items.AddRange(results);
            }

        }

        // This method will set properties on the PrintDialog object and
        // then display the dialog.
        private void button2_Click(object sender, EventArgs e)
        {
            // Allow the user to choose the page range he or she would
            // like to print.
            printDialog1.AllowSomePages = true;

            // Show the help button.
            printDialog1.ShowHelp = true;

            // Set the Document property to the PrintDocument for 
            // which the PrintPage Event has been handled. To display the
            // dialog, either this property or the PrinterSettings property 
            // must be set 
            printDialog1.Document = docToPrint;

            DialogResult result = printDialog1.ShowDialog();

            // If the result is OK then print the document.
            if (result == DialogResult.OK)
            {
                docToPrint.Print();
            }
        }

        // The Click event is raised when the user clicks the Print button.
        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                streamToPrint = new StreamReader
                   (Environment.GetFolderPath(Environment.SpecialFolder.Desktop)+"\\MyFile.txt");
                try
                {
                    printFont = new Font("Arial", 10);
                    PrintDocument pd = new PrintDocument();
                    pd.PrintPage += new PrintPageEventHandler
                       (this.pd_PrintPage);
                    pd.Print();
                }
                finally
                {
                    streamToPrint.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        // The PrintPage event is raised for each page to be printed.
        private void pd_PrintPage(object sender, PrintPageEventArgs ev)
        {
            float linesPerPage = 0;
            float yPos = 0;
            int count = 0;
            float leftMargin = ev.MarginBounds.Left;
            float topMargin = ev.MarginBounds.Top;
            string line = null;

            // Calculate the number of lines per page.
            linesPerPage = ev.MarginBounds.Height /
               printFont.GetHeight(ev.Graphics);

            // Print each line of the file.
            while (count < linesPerPage &&
               ((line = streamToPrint.ReadLine()) != null))
            {
                yPos = topMargin + (count *
                   printFont.GetHeight(ev.Graphics));
                ev.Graphics.DrawString(line, printFont, Brushes.Black,
                   leftMargin, yPos, new StringFormat());
                count++;
            }

            // If more lines exist, print another page.
            if (line != null)
                ev.HasMorePages = true;
            else
                ev.HasMorePages = false;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            var sw=File.CreateText(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\MyFile.txt");
            sw.Write("This is test file.");
            sw.Flush();
            sw.Dispose();
        }

        private void button3_Click_1(object sender, EventArgs e)
        {
            document.DocumentName = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\MyFile.txt";

            // Set the PrintPreviewDialog.Document property to
            // the PrintDocument object selected by the user.
            printPreviewDialog1.Document = document;

            // Call the ShowDialog method. This will trigger the document's
            //  PrintPage event.
            printPreviewDialog1.ShowDialog();
        }
    }
}
